<?php

namespace app\admin\controller;

use app\admin\model\Store;
use app\admin\model\User;
use app\admin\model\Storetradedetail;

class Index extends Base
{

    public function index(){
        return $this->fetch('layout/layout');
    }

    public function dashboard(){
        $countData = $this->getCountData();

        $this->assign('countData', $countData);

        return $this->fetch('index/index');
    }

    /**
     * 今日&当月 统计数据
     */
    private function getCountData(){
        $startDay = strtotime(date('Y-m-d'), time());
        //$startMonth = strtotime(date("Y-m-01"));
        //$endMonth = strtotime(date("Y-m-01 23:59:59")." +1 month -1 day");

        $where = [];
        // 商户统计
        // 今日商户
        $todayStore = Store::whereTime('createDate', 'today')->count();
        // 当月累计
        $monthStore = Store::whereTime('createDate', 'month')->count();

        // 用户统计
        // 今日用户
        $todayUser = User::whereTime('createDate', 'today')->count();
        // 当月累计
        $monthUser = User::whereTime('createDate', 'month')->count();

        // 交易额统计
        // 今日交易额
        $todayTrade = Storetradedetail::whereTime('payTime', 'today')->count();
        // 当月交易额
        $monthTrade = Storetradedetail::whereTime('payTime', 'month')->count();

        $countData = [
            'todayStore' => $todayStore ?: 0,
            'monthStore' => $monthStore ?: 0,
            'todayUser' => $todayUser ?: 0,
            'monthUser' => $monthUser ?: 0,
            'todayTrade' => $todayTrade ?: 0,
            'monthTrade' => $monthTrade ?: 0,
        ];

        return $countData;
    }
}